package com.sczmgk.hotline_pzh.broadcase;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

import com.sczmgk.hotline_pzh.utils.MyLog;

/**
 * Created by Administrator on 2016/11/16.
 */
public class PhoneStateBroadCastReceiver extends BroadcastReceiver
{
    private static final String TAG = "PhoneStateBroadCastReceiver";

    private static boolean mIncomingFlag = false;
    private static String mIncomingNumber = null;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        // 如果是拨打电话
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL))
        {
            mIncomingFlag = false;
            String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            MyLog.i(TAG, "call OUT:" + phoneNumber);

        } else
        {
            // 如果是来电
            TelephonyManager tManager =
                    (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
            switch (tManager.getCallState())
            {
                case TelephonyManager.CALL_STATE_RINGING:
                    mIncomingNumber = intent.getStringExtra("incoming_number");
                    MyLog.i(TAG, "RINGING :" + mIncomingNumber);
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    if (mIncomingFlag)
                    {
                        MyLog.i(TAG, "incoming ACCEPT :" + mIncomingNumber);
                    }
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    if (mIncomingFlag)
                    {
                        MyLog.i(TAG, "incoming IDLE");
                    }
                    break;
            }
        }
    }

}